package com.suda.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suda.dao.PackagesMapper;
import com.suda.pojo.Packages;
import com.suda.service.PackagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:zhuyongjie
 * @Description:Packages业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class PackagesServiceImpl implements PackagesService {

    @Autowired
    private PackagesMapper packagesMapper;


    /**
     * Packages条件+分页查询
     * @param packages 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Packages> findPage(Packages packages, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(packages);
        //执行搜索
        return new PageInfo<Packages>(packagesMapper.selectByExample(example));
    }

    /**
     * Packages分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Packages> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Packages>(packagesMapper.selectAll());
    }

    /**
     * Packages条件查询
     * @param packages
     * @return
     */
    @Override
    public List<Packages> findList(Packages packages){
        //构建查询条件
        Example example = createExample(packages);
        //根据构建的条件查询数据
        return packagesMapper.selectByExample(example);
    }


    /**
     * Packages构建查询对象
     * @param packages
     * @return
     */
    public Example createExample(Packages packages){
        Example example=new Example(Packages.class);
        Example.Criteria criteria = example.createCriteria();
        if(packages!=null){
            // 包裹id
            if(!StringUtils.isEmpty(packages.getPackageId())){
                    criteria.andEqualTo("packageId",packages.getPackageId());
            }
            // 体积
            if(!StringUtils.isEmpty(packages.getVolumn())){
                    criteria.andEqualTo("volumn",packages.getVolumn());
            }
            // 重量
            if(!StringUtils.isEmpty(packages.getWeight())){
                    criteria.andEqualTo("weight",packages.getWeight());
            }
            // 长x宽x高
            if(!StringUtils.isEmpty(packages.getLenWidHigh())){
                    criteria.andEqualTo("lenWidHigh",packages.getLenWidHigh());
            }
            // 数量
            if(!StringUtils.isEmpty(packages.getQuantity())){
                    criteria.andEqualTo("quantity",packages.getQuantity());
            }
            // 包裹内容
            if(!StringUtils.isEmpty(packages.getPackageContent())){
                    criteria.andEqualTo("packageContent",packages.getPackageContent());
            }
            // 时效类型
            if(!StringUtils.isEmpty(packages.getAgingType())){
                    criteria.andEqualTo("agingType",packages.getAgingType());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        packagesMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Packages
     * @param packages
     */
    @Override
    public void update(Packages packages){
        packagesMapper.updateByPrimaryKey(packages);
    }

    /**
     * 增加Packages
     * @param packages
     */
    @Override
    public void add(Packages packages){
        packagesMapper.insert(packages);
    }

    /**
     * 根据ID查询Packages
     * @param id
     * @return
     */
    @Override
    public Packages findById(String id){
        return  packagesMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Packages全部数据
     * @return
     */
    @Override
    public List<Packages> findAll() {
        return packagesMapper.selectAll();
    }
}
